package com.mayuanfei.springboot15.controller;

import cn.hutool.core.date.DateUtil;
import com.hazelcast.core.HazelcastInstance;
import com.hazelcast.map.IMap;
import com.mayuanfei.springboot15.common.Constants;
import com.mayuanfei.springboot15.pojo.UserInfo1;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Map;

/**
 * Jdk自带序列化器
 *
 * @author 老马
 * @date 2023-07-10 16:23
 */
@RestController
public class JdkSerializableController {

    @Resource
    private HazelcastInstance hazelcastInstance;

    @PostMapping(value = "/writeJdkSerializable")
    public UserInfo1 writeDataToHazelcast(String userId, String userName, Integer age) {
        UserInfo1 userInfo = new UserInfo1();
        userInfo.setUserId(userId);
        userInfo.setUserName(userName);
        userInfo.setAge(age);
        userInfo.setBirthday(DateUtil.date().toJdkDate());
        IMap<String, Object> map = hazelcastInstance.getMap(Constants.CACHE_NAME_SESSION_USERS);
        map.put(userId, userInfo);
        return userInfo;
    }

    @GetMapping(value = "/readOneJdkSerializable")
    public UserInfo1 readDataFromHazelcast(String userId) {
        IMap<String, UserInfo1> map = hazelcastInstance.getMap(Constants.CACHE_NAME_SESSION_USERS);
        return map.get(userId);
    }

    @GetMapping(value = "/readAllJdkSerializable")
    public Map<String, UserInfo1> readAllDataFromHazelcast() {
        return hazelcastInstance.getMap(Constants.CACHE_NAME_SESSION_USERS);
    }

}